package com.lagou.edu.service.impl;

import com.lagou.edu.dao.UserDao;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * Created by Heweipo on 2021/2/15.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getOne(Long id) {
        return userDao.getOne(id);
    }

    @Override
    public boolean isExistByEmail(String email) {
        User user = new User();
        user.setEmail(email);
        Example<User> example = Example.of(user);
        Optional<User> one = userDao.findOne(example);
        return one.isPresent();
    }

    @Override
    public User findOneByEmailAndPwd(String email, String pwd) {
        User user = new User();
        user.setEmail(email);
        user.setPwd(pwd);
        Example<User> example = Example.of(user);
        Optional<User> one = userDao.findOne(example);
        return one.orElse(null);
    }

    @Override
    public User saveUser(User user) {
        return userDao.save(user);
    }
}
